import * as fs from 'fs/promises';
import * as path from 'path';

export async function deleteFolderRecursive(dirPath) {
  try {
    // 获取目录下所有内容（包括文件和子目录）
    const entries = await fs.readdir(dirPath, { withFileTypes: true });

    for (const entry of entries) {
      const fullPath = path.join(dirPath, entry.name);

      if (entry.isDirectory()) {
        // 如果是子目录，则递归删除该子目录
        await deleteFolderRecursive(fullPath);
      } else {
        // 如果是文件，则直接删除文件
        await fs.unlink(fullPath);
      }
    }

    // 删除当前空目录
    await fs.rmdir(dirPath);
    return '删除成功';
  } catch (err) {
    console.error(`Failed to delete folder ${dirPath}:`, err);
  }
}
